import { IFeidaoAiBrowserComponent, IFeidaoAiBrowserPage } from '../../../interfaces';

/**
 * 获取微信公众平台静默授权code
 * @param fd 页面响应fd
 * @param appid 微信公众平台appid
 * @example get_code(fd: IFeidaoAiBrowserPage, appid);
 */
export default function get_code(fd: IFeidaoAiBrowserPage | IFeidaoAiBrowserComponent, appid: string) {
	const code = fd.data.params.code;
	if (!code) {
		const url = window.location.href;
		// 重定向
		window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + encodeURIComponent(url) + '&response_type=code&scope=snsapi_base&state=#wechat_redirect';
	} else {
		return code;
	}
}
